/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.others.managed;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.others.bean.Login;
import sv.com.intesal.others.controlador.LoginControlador;

/**
 *
 * @author JM
 */
@ManagedBean(name = "loginManaged")
@ViewScoped
public class LoginManaged extends ViewScopedAbstract {

    private Login selectedLogin;

    public LoginManaged() {
        this.selectedLogin = new Login();
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;

            if (this.selectedLogin.getUser_l().equals("")) {
                m.messageAdvertencia(m.campoError("USUARIO"));
                var_return = false;
            }
            if (this.selectedLogin.getPassword_l().equals("")) {
                m.messageAdvertencia(m.campoError("CLAVE"));
                var_return = false;
            }
            
            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                LoginControlador lc = new LoginControlador();
                if (lc.validarLogin(this.selectedLogin) == false) {
                    m.messageAdvertencia(m.campoError("USUARIO Y/O CLAVE INVALIDO"));
                    this.selectedLogin.setPassword_l("");
                    this.selectedLogin.setUser_l("");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    /*SETTERS AND GETTERS*/
    public Login getSelectedLogin() {
        return selectedLogin;
    }

    public void setSelectedLogin(Login selectedLogin) {
        this.selectedLogin = selectedLogin;
    }
}
